package com.cms.admin.action.patient;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.*;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.patient.PatientService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/4/26.
 */
public class PatientManagetAction extends BaseAction implements ModelDriven<PatientListExt> {

    private PatientService patientService;
    private TemplateService templateService;
    private List<PatientList> patientList;
    private PatientListExt search = new PatientListExt();
    private PageBean page;


    public String ajaxQueryPatient() {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        page = new PageBean(search.getCurrentPage(), search.getPageSize());
        patientList = patientService.getPatient(page, search);
        LOG.info("ajaxQueryPatient", "查询到的就就诊人数量:" + patientList.size());

        String res = null;
        try {
            res = this.getQueryResult(patientList, page);
            JSONObject o = new JSONObject();
            o.put("pageCount", page.getTotalPage());
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }


    public String ajaxQueryPatientForSelect()
    {

        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        page = new PageBean(search.getCurrentPage(), search.getPageSize());
        patientList = patientService.getPatient(page, search);
        LOG.info("ajaxQueryPatient", "查询到的就就诊人数量:" + patientList.size());

        String res = null;
        try {
            res = this.getQueryResultForSelect(patientList);
            JSONObject o = new JSONObject();

            o.put("pageCount", page.getTotalPage());
            o.put("content", res);

            return ajaxJsonSuccessMessage(o);

        } catch (Exception e) {
            LOG.error("ajaxQueryPatientForSelect",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    private String getQueryResult(List<PatientList> patientList, PageBean page)
            throws Exception {

        HashMap map = new HashMap();
        map.put("patientList", patientList);
        map.put("page", page);

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_PATIENT_QUERY_RESULT, map);
        return result;
    }

    private String getQueryResultForSelect(List<PatientList> patientList)
            throws Exception {

        HashMap map = new HashMap();
        map.put("patientList", patientList);

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_PATIENT_QUERY_FOR_SELECT_RESULT, map);
        return result;
    }

    @Override
    public PatientListExt getModel() {
        return search;
    }

    public PatientService getPatientService() {
        return patientService;
    }

    public void setPatientService(PatientService patientService) {
        this.patientService = patientService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }
}
